<template>
  <div id="building">
    <div class="register">
      <el-form
        :model="ruleForm2"
        status-icon
        :rules="rules2"
        ref="ruleForm2"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm2.name"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nick">
          <el-input v-model="ruleForm2.nick"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="ruleForm2.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm2.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm2.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm2')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm2')">重置</el-button>
          
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { RegisterApi } from "../request/api.js";
import Cookie from "js-cookie";
export default {
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("输入内容不得为空"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm2: {
        name:'',
        nick:"",
        phone:"",
        pass: "",
        checkPass: "",
      },
      rules2: {
        name:[{validator:validatePass,trigger:"blur"}],
        nick:[{validator:validatePass,trigger:"blur"}],
        phone:[{validator:validatePass,trigger:"blur"}],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
            RegisterApi({name:this.ruleForm2.name,nick:this.ruleForm2.nick,phone:this.ruleForm2.phone,password:this.ruleForm2.pass,rePass:this.ruleForm2.checkPass}).then((res)=>{
                console.log(res);
            })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped lang="less">
#building{
  background: url("../assets/R-C.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  .register{
    width: 400px;
    position: absolute;
    right: 200px;
    top: 100px;
    border: 1px solid #333;
    padding: 20px;
    box-sizing: border-box;
}
}

</style>